package com.example.week12_networkprogramming;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClientActivity extends AppCompatActivity {

    private TextView chatmessage=null;

    private EditText sendmessage=null;
    private static final String HOST="10.21.109.54";
    private static final int PORT=29898;
    private Socket socket=null;
    private BufferedReader bufferedReader=null;
    private PrintWriter printWriter=null;
    private String msg="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket_client);

        chatmessage=(TextView)findViewById(R.id.chatmessage);
        sendmessage=(EditText)findViewById(R.id.sendmessage);

        new Thread()
        {
            public void run()
            {
                try
                {
                    socket=new Socket(HOST,PORT);
                    bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    printWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
                 super.run();
            }
        }.start();

        findViewById(R.id.sendbutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message=sendmessage.getText().toString();
                Log.d("TAG","将发送1："+message);
                if(socket!=null&&printWriter!=null)
                {
                    if(socket.isConnected())
                    {
                        if(!socket.isOutputShutdown())
                        {
                            printWriter.println(message);
                            printWriter.flush();
                            chatmessage.setText(chatmessage.getText().toString()+"\n"+"发送: "+message);
                            sendmessage.setText("");
                        }
                    }
                }
            }
        });

        new Thread()
        {
            public void run()
            {
                while (true)
                {
                    if(socket!=null&&bufferedReader!=null)
                    {
                        if(!socket.isInputShutdown())
                        {
                            try
                            {
                                if((msg=bufferedReader.readLine())!=null)
                                {
                                    Log.i("TAG",msg);
                                    chatmessage.setText(chatmessage.getText().toString()+"\n"+"接收: "+msg);
                                }
                            }catch (Exception ex)
                            {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            }

        }.start();
    }

}
